package org.example.springbook.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.springbook.model.BookInfo;
import org.example.springbook.model.PageRequest;
import org.example.springbook.model.PageResult;
import org.example.springbook.model.Result;
import org.example.springbook.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
// 业务逻辑层
@Slf4j
@RequestMapping("/book")
@ResponseBody
@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest) {
        log.info("查询列表信息,pageRequest={}", pageRequest);
        if(pageRequest.getCurrentPage()<1) {
            return Result.error("参数错误");
        }
        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/getList")
    public List<BookInfo> getList() {
        // 返回数据
        return bookService.getBookList();
    }

    @RequestMapping("/addBook")
    public Result addBook(BookInfo book) {
        log.info("添加图书:{}", book);
        // 参数校验
        if(!StringUtils.hasLength(book.getBookName())
                ||!StringUtils.hasLength(book.getAuthor())
                ||!StringUtils.hasLength(book.getPublish())
                ||book.getCount()<=0
                ||book.getPrice()==null) {
            return Result.error("参数错误");
        }
        // 添加图书
        try {
            bookService.insertBook(book);
        } catch (Exception e) {
            return Result.error("添加图书失败",e);
        }
        return Result.success("");
    }

    @RequestMapping("/queryBookById")
    public Result queryBookById(Integer bookId) {
        log.info("查询图书信息，bookId:{}", bookId);
        if(bookId<1) {
            log.error("非法图书Id，bookId:" + bookId);
            return Result.error("非法图书Id",bookId);
        }
        return Result.success(bookService.queryBookById(bookId));
    }

    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo book) {
        log.info("更新图书信息，bookL:{}", book);
        // 参数校验
        if(book.getId()<1) {
            return Result.error("参数错误");
        }
        try {
            Integer result = bookService.updateBook(book);
            if(result<=0) {
                return Result.error("更新图书失败");
            }
        } catch (Exception e) {
            log.error("更新图书失败，e:{}", e);
            return Result.error("更新图书失败",e);
        }
        return Result.success(true);
    }

    @RequestMapping("/batchDelete")
    public Result batchDelete(@RequestParam List<Integer> ids) {
        if(ids==null || ids.size()<1) {
            return Result.error("参数错误");
        }
        try {
            Integer result = bookService.batchDelete(ids);
            if (result <= 0) {
                return Result.error("内部错误");
            }
        } catch (Exception e) {
            log.error("删除图书失败，e:{}", e);
            return Result.error("删除图书失败",e);
        }
        return Result.success(true);
    }
}
